package com.tuijian.service.filter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tuijian.service.fineranking.FineRankingService;
import com.tuijian.util.FilterInterface;
import com.tuijian.util.HbaseUtils;
import com.tuijian.vo.CellData;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

@Service
public class FilterService {
    @Resource
    FineRankingService fineRanking;
    @Resource
    private HbaseUtils hbaseUtils;

    @Resource
    private FilterInterface filterImpl;

    public void filterMethod(String userId,String dataType) {
        try {
            hbaseUtils.connect("hadoop1,hadoop2,hadoop3","2181");
            System.out.println("connect success!");
            System.out.println("----------------------------------------------------------------");
            filterProcess(userId,dataType,filterImpl);
            System.out.println("----------------------------------------------------------------");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                hbaseUtils.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 对召回后的数据进行过滤，返给精排处理
     * @param userId 查询用户id
     * @param dataType 查询数据类型： 0-查询帖子  1-查询商品
     * @throws Exception
     */
    public void filterProcess(String userId, String dataType, FilterInterface impl) throws Exception {
        BinaryComparator comparator = new BinaryComparator(Bytes.toBytes(userId));
        List<CellData[]> resList = null;
        if("1".equals(dataType)){
            resList=hbaseUtils.filter("product_recall", "info", null,null,"null",
                    "=", comparator);
        }else if("0".equals(dataType)){
            resList=hbaseUtils.filter("card_recall", "info", null,null,"null",
                    "=", comparator);
        }
        for(CellData[] arr : resList){
            for(CellData item : arr){
                String rawList=(String)item.getValue();
                JSONObject json = JSON.parseObject(rawList);
                HashMap<String,Double> map=new HashMap<String,Double>();
                Set<String> ids = json.keySet();
                ids.forEach(id ->{
                    map.put(id,json.getDoubleValue(id));
                });
                //按照过滤规则对召回结果进行过滤
                List<String> excludeList = impl.filter(userId, dataType);
                if(excludeList!=null && excludeList.size()!=0) {
                    for(String exclude : excludeList){
                        map.remove(exclude);
                    }
                }
                System.out.println(userId+":"+dataType+":"+map);
                //调用精排方法，传入userId,dataType,map
                fineRanking.fineRanking(userId,dataType,map);


            }
        }
    }
}
